AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")


function ENT:SpawnFunction(ply, tr) -- Spawn function needed to make it appear on the spawn menu
	if (!tr.HitWorld) then return end
 
	local ent = ents.Create("weed_compressor") -- Create the entity
	ent:SetPos(tr.HitPos + Vector(0, 0, 28)) -- Set it to spawn 50 units over the spot you aim at when spawning it
	ent:Spawn() -- Spawn it 
		
		return ent -- You need to return the entity to make it work
end

function ENT:Initialize()
	util.AddNetworkString("startTheBar")

	self:SetModel("models/props_wasteland/laundry_washer003.mdl")
	self:PhysicsInit(SOLID_VPHYSICS)
	self:SetMoveType(MOVETYPE_VPHYSICS)
	self:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)



	local phys = self:GetPhysicsObject()
	self.nodupe = true
	self.ShareGravgun = true

	phys:SetMass(250)
	phys:Wake()

	self.plate = ents.Create("prop_physics")
	self.plate:SetModel("models/props_phx/construct/windows/window1x1.mdl")
	self.plate:SetPos(self.Entity:GetPos() - self.Entity:GetAngles():Up()*20 - self.Entity:GetAngles():Right()*22)
	self.plate:Spawn()
	//self.plate:PhysicsInit(SOLID_VPHYSICS)
	//self.plate:SetMoveType(MOVETYPE_VPHYSICS)
	//self.plate:SetSolid(SOLID_VPHYSICS)
	//self.plate:SetUseType(SIMPLE_USE)
	self.plate:SetParent(self.Entity)
	self.plate:SetCollisionGroup( COLLISION_GROUP_WORLD )

	self.Weed = 0
	self.LastEnt = nil
	self.IsWorking = false
	self.CompactTimer = 20
	self.WeedNeeded = 15

end


function ENT:Use(usr,caller)

	if !self.IsWorking and self.Weed >= self.WeedNeeded then
		self.Weed = self.Weed - self.WeedNeeded
		self:Setweedbags(self.Weed)

		net.Start("startTheBar")
			net.WriteEntity(self.Entity)
			net.WriteString(tostring(self.CompactTimer))
		net.Broadcast()

		self.StartTime=CurTime()
		self.EndTime = CurTime() + self.CompactTimer
		self.IsWorking=true
	end
end

function ENT:Touch(hitEnt)
	if self.LastEnt == hitEnt then return end
	self.LastEnt = hitEnt


	if hitEnt:GetClass() == "durgz_weed" then
		hitEnt:Remove()
		self.Weed = self.Weed + 1
		self:Setweedbags(self.Weed)
	end
end

function ENT:Think()
	if self.IsWorking and CurTime() >= self.EndTime then
		self.IsWorking=false

		self.ball = ents.Create("hash_ball")
		self.ball:SetModel("models/props_junk/rock001a.mdl")
		self.ball:SetPos(self.Entity:GetPos() - self.Entity:GetAngles():Up()*10 - self.Entity:GetAngles():Right()*35)
		self.ball:Spawn()
		self.ball:PhysicsInit(SOLID_VPHYSICS)
		self.ball:SetMoveType(MOVETYPE_VPHYSICS)
		self.ball:SetSolid(SOLID_VPHYSICS)
		self.ball:SetUseType(SIMPLE_USE)
	end
end